using System;
using System.Threading;

public static class Program {

   public static void Main() {
   }
}

internal sealed class CacheCoherencyProblem {
   private Byte  m_initialized = 0;
   private Int32 m_value = 0;

   // This method is executed by one thread
   private void Thread1() {
      m_value = 5;
      m_initialized = 1;
   }

   // This method is executed by another thread
   private void Thread2() {
      if (m_initialized == 1) {
         // This may execute and display 0
         Console.WriteLine(m_value);
      }
   }
}

internal sealed class VolatileMethods {
   private Byte  m_initialized = 0;
   private Int32 m_value = 0;

   // This method is executed by one thread
   private void Thread1() {
      m_value = 5;
      Thread.VolatileWrite(ref m_initialized, 1);
   }

   // This method is executed by another thread
   private void Thread2() {
      if (Thread.VolatileRead(ref m_initialized) == 1) {
         // If we get here, 5 will be displayed
         Console.WriteLine(m_value);
      }
   }
}

internal sealed class VolatileField {
   private volatile Byte m_initialized = 0;
   private Int32 m_value = 0;

   // This method is executed by one thread
   private void Thread1() {
      m_value = 5;
      m_initialized = 1;
   }

   // This method is executed by another thread
   private void Thread2() {
      if (m_initialized == 1) {
         // If we get here, 5 will be displayed
         Console.WriteLine(m_value);
      }
   }
   private void CantPassVolatileFieldByReference() {
      // Warning CS0420: a reference to a volatile field 
      // will not be treated as volatile
      Boolean success = Byte.TryParse("123", out m_initialized);
   }
}